package com.ql.gamelogm.dao.impl;

import com.ql.gamelogm.dao.CalendarDao;
import com.ql.gamelogm.entities.CalendarEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class CalendarDaoImpl implements CalendarDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public List<CalendarEvent> findCalsById(String id) {
        String sql="select * from calendar where id='"+id+"'";
        List<CalendarEvent> calendarEventList = jdbcTemplate.query(sql, (resultSet, rowNum) -> new CalendarEvent(
                resultSet.getInt("ckey"),
                resultSet.getString("id"),
                resultSet.getString("content"),
                resultSet.getTimestamp("date")
        ));
        return calendarEventList;
    }

    @Override
    public boolean updateCalsById(String date, String id , String content) {
        String sql="update calendar set content=? where id='"+id+"' and date='"+date+"'";
        int update = jdbcTemplate.update(sql,content);
        if(update==0){
            return false;
        }
        return true;

    }

    @Override
    public boolean addCals(String id, String content, String date) {
        String sql="insert into calendar(id,content,date) values(?,?,?)";
        int update = jdbcTemplate.update(sql,id,content,date);
        if(update==0){
            return false;
        }
        return true;
    }
}
